home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / AEObject-Edition1.0.2 Sample / AEObject.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-09  |  79.5 KB  |  1,781 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. *
  3. *  Apple Developer Technical Support
  4. *
  5. *  AppleEvent™ Object Model and Object Support Library routines
  6. *
  7. *  Program:AEObject-Edition Sample
  8. *  File: AEObject.c -C Source
  9. *
  10. *  by:   C.K. Haun <TR>
  11. *
  12. *  Copyright © 1991,1992 Apple Computer, Inc.
  13. *  All rights reserved.
  14. *
  15. *------------------------------------------------------------------------------
  16. * This file contains most of the routines that deal with AppleEvent Objects, and
  17. * the routines that use them.
  18. * Object resolution, and Object accessors play a large part here.  Once you under-
  19. * stand and have written a few accessors, you'll see that this stuff becomes a great
  20. * deal easier.
  21. * vers 1.0.1- All AE constants changed to reflect Winter '92 registry 
  22. *----------------------------------------------------------------------------*/
  23. /* And another Note:
  24. * I am resolving simple objects in this file and sample.  I intend to
  25. * go on to complex objects in a future sample, please stay tuned */
  26.  
  27. /* Some words about the objects and handlers */
  28. /* Windows */
  29. /* When a window is asked for or used, the token I create is an AEDesc */
  30. /* of type cWindow, and the data associated with this is  */
  31. /* (suprisingly enough) a window pointer.  In this sample I can do anything */
  32. /* with my data based on a window pointer. */
  33. /* The rest of the tokens are AEDescs containing structures in the dataHandle, */
  34. /* please see the AESampStructs.h file for the definition of these structures */
  35. /* ••• Remember ---  A token can be anything you like. It does NOT get passed outside */
  36. /* your application, it only stays inside it.   */
  37. #define __AEOBJ__
  38.  
  39. #include "Sampdefines.h"
  40.  
  41. #pragma segment MyInit
  42.  
  43. /* InstallObjectHandlers normally would be in the Initialize.c file, but I wanted to put it here, */
  44. /* along with the rest of the Object Model code so you could see everything in one place */
  45.  
  46. /* This routine starts the ObjectSupportLibrary, and installs all the callbacks and  */
  47. /* object accessors for this program. */
  48. OSErr InstallObjectHandlers(void)
  49. {
  50.     /* Initialize the library.  This makes some internal calls to the AppleEvent manager */
  51.     OSErr myErr = AEObjectInit();
  52.     if (!myErr) {
  53.         /* The meat of the initialization.  Here I'm installing all the object accessors */
  54.         /* I've written.  These are the routines that the OSL will call when I call */
  55.         /* AEResolve on an object in an AppleEvent.  These routines do all the  */
  56.         /* parsing of the object specifier, segment by segment.  Each of them returns a */
  57.         /* token that describes the in-memory structure of the object specified in a way */
  58.         /* my application can understand.  No one else will ever see these tokens */
  59.         /* they are ONLY for use inside my program.  This means that they can be anything that */
  60.         /* makes sense to me and my appliction. */
  61.         /* The actual token structures I use are defined in the AESampStructs.h file. */
  62.         /* This accessor returns a window token from a null descriptor.  This  */
  63.         /* accessor will be called a LOT, since specifing anything in my application */
  64.         /* will usually reference an owning window. */
  65.         
  66.         myErr = AEInstallObjectAccessor(cWindow, typeNull, (accessorProcPtr)WindFromNull, 0, false);
  67.         
  68.         /* This accessor gets ANY property from a window token */
  69.         /* Since a window token describes all the important junk about the window */
  70.         /* I only need one property from window accessor */
  71.         
  72.         myErr = AEInstallObjectAccessor(typeProperty, cWindow, (accessorProcPtr)PropertyFromWindow, 0, false);
  73.         
  74.         /* This installs my Text from Window accessor.  If there is a text object in this window */
  75.         /* this routine will return a token for it */
  76.         
  77.         myErr = AEInstallObjectAccessor(cText, cWindow, (accessorProcPtr)TextFromWindow, 0, false);
  78.         
  79.         /* Now be able to pass back properties of my Text objects */
  80.         
  81.         myErr = AEInstallObjectAccessor(typeProperty, cText, (accessorProcPtr)PropertyFromText, 0, false);
  82.         
  83.         /* This installs my Word from Text accessor. */
  84.         
  85.         myErr = AEInstallObjectAccessor(cWord, cText, (accessorProcPtr)WordFromText, 0, false);
  86.         
  87.         myErr = AEInstallObjectAccessor(cDrawingArea, cWindow, (accessorProcPtr)DrawFromWindow, 0, false);
  88.         
  89.         /* This guy graps a graphic shape from my window, no matter what it actually is */
  90.         
  91.         myErr = AEInstallObjectAccessor(cGraphicShape, cDrawingArea, (accessorProcPtr)ShapeFromDraw, 0, false);
  92.         myErr = AEInstallObjectAccessor(cGraphicShape, cWindow, (accessorProcPtr)ShapeFromDraw, 0, false);
  93.         /* these grab specific shapes from the drawing area */
  94.         /* The canny observer will notice that these all call the ShapeFromDraw routine */
  95.         /* Since my shapes are all defined in the same type of structure, this makes  */
  96.         /* a lot of sense for me */
  97.                 
  98.         myErr = AEInstallObjectAccessor(cGraphicLine, cDrawingArea, (accessorProcPtr)ShapeFromDraw, 0, false);
  99.         myErr = AEInstallObjectAccessor(cOval, cDrawingArea, (accessorProcPtr)ShapeFromDraw, 0, false);
  100.         myErr = AEInstallObjectAccessor(cRectangle, cDrawingArea, (accessorProcPtr)ShapeFromDraw, 0, false);
  101.         myErr = AEInstallObjectAccessor(cGraphicLine, cWindow, (accessorProcPtr)ShapeFromDraw, 0, false);
  102.         myErr = AEInstallObjectAccessor(cOval, cWindow, (accessorProcPtr)ShapeFromDraw, 0, false);
  103.         myErr = AEInstallObjectAccessor(cRectangle, cWindow, (accessorProcPtr)ShapeFromDraw, 0, false);
  104.  
  105.         /* and the same type o' thing for shape properties */
  106.         myErr = AEInstallObjectAccessor(typeProperty, cGraphicLine, (accessorProcPtr)PropertyFromShape, 0, false);
  107.         myErr = AEInstallObjectAccessor(typeProperty, cOval, (accessorProcPtr)PropertyFromShape, 0, false);
  108.         myErr = AEInstallObjectAccessor(typeProperty, cRectangle, (accessorProcPtr)PropertyFromShape, 0, false);
  109.  
  110.  
  111.         
  112.         /* And the list will go on and on.  Once you've written a few accessors, you realize  */
  113.         /* how handy they are, and you start installing them for everything that can  */
  114.         /* possibly be gotten from anything else.  The only limit will be the  */
  115.         /* amount of code space you want to dedicate to accessors. */
  116.         /* Remember, the more accessors you put in, the more ways users will have to */
  117.         /* get at your data, and the less they will have to learn or remember. */
  118.         /* Our goal, as always, is to make it as easy for the user as possible. */
  119.         /* I will also use the same routine to go from a null */
  120.         
  121.         /* Here I am installing all the call back routines that the OSL needs.  */
  122.         /* These are not all implemented as doing something, but they will eventually */
  123.         /* These tokens will be called by the OSL as it does it's parsing  */
  124.         /* thrugh a passed object specifier. */
  125.         /* You may not need all of these, any you don't need just pass */
  126.         /* a nil */
  127.         
  128.         myErr = AESetObjectCallbacks((compareProcPtr)nil, 
  129.             (countProcPtr)MyCountProc, 
  130.             (disposeTokenProcPtr)MyDisposeTokenProc,
  131.             (getMarkTokenProcPtr)nil,
  132.             (markProcPtr)nil, 
  133.             (adjustMarksProcPtr)nil, 
  134.             (getErrDescProcPtr)nil);
  135.         
  136.     }
  137.     return(myErr);
  138. }
  139.  
  140.  
  141. #pragma segment AEOSL
  142. /* My GetData event handler.  This routine resolves the object passed (calling AEResolve) */
  143. /* and then returns the appropriate data to the sender of the event */
  144. pascal OSErr AEGetDataHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  145. {
  146.  #pragma unused (refIn)
  147.  
  148.     OSErr myErr = noErr;
  149.     AEDesc tokenBack;
  150.     AEDesc theObject;
  151.     AEDesc returnDesc;
  152.     /* variables for the various tokens I'll be getting back */
  153.     CWordObjHandle wordToken;
  154.     CTextObjHandle textToken;
  155.     PropertyTHdl propertyToken;
  156.     Handle myDataHandle, tempHandle;
  157.     Size dataSize;
  158.     /* window is just a window ptr */
  159.     gCurrentReply = reply;
  160.     mVerboseOutput("\p\nEntering GetData handler")
  161.     myErr = AEGetKeyDesc(messagein, keyDirectObject, typeObjectSpecifier, &theObject);
  162.         mAEErrorDisplay("\p getting object spec", myErr)
  163.     if (!myErr) {
  164.         returnDesc.descriptorType = typeNull;               /* set this up for a later check */
  165.         myErr = AEResolve(&theObject, kAEIDoMinimum, &tokenBack);
  166.         mAEErrorDisplay("\p AEResolve", myErr)
  167.         
  168.         if (!myErr) {
  169.             /* Hey, marvy, we got an object token back.  That means that we have  */
  170.             /*  enough information to derive the data. */
  171.             /* Again, and forever, we don't care how you actually access data and create tokens */
  172.             /* inside your application, you know how to do that much bettern than we ever will. */
  173.             /* As long as it makes sense, and is efficient, for you, it's fine with us. */
  174.             /* I am going to have to do some switching and stuff here to put the data I got into the reply */
  175.             
  176.             /* By the Way:  You could make all your tokens with the same field for the  */
  177.             /* data and descriptor type.  If you did that, you wouldn't need the case */
  178.             /* here for a GetData event, you would just pass back what you got. */
  179.             /* I thought about it for this sample, but it seems more descriptive to */
  180.             /* go through the specific objects.  I may change my mind.... */
  181.             
  182.             /* Anyway, I switch off the type of token I got back... */
  183.             switch (tokenBack.descriptorType) {
  184.                 case cWindow:
  185.                     /* for this, I'm going to pass back the Window ID I use in this program. */
  186.                     /* Why not the window pointer?  Because we don't have shared memory in the current */
  187.                     /* Mac OS, so I don't want people mucking around in my windows directly. */
  188.                     /* And really, if you're getting a request for a bare window, all the other side */
  189.                     /* is probably trying to see is if a window exisits.  I hope. */
  190.                     /* they can pass back this as a selector for NewElement, or something */
  191.                     /* You won't see this for a GetData much, in fact you can't from this  */
  192.                     /* sample get it.  I mean it ain't in the GetData menu */
  193.                     
  194.                     break;
  195.                 case cText:
  196.                     /* Pass back the chunk o' text.  Since I'm using TextEdit, I know that I'll */
  197.                     /* never be over 32k here.  Keep in mind that the AppleEvent manager can only */
  198.                     /* handle data up to 64k, so if you are passing back more text (or whatever) */
  199.                     /* than that you'll have to do it in a series of returns, or pass back an */
  200.                     /* alias to a file, or some such. */
  201.                     textToken = (CTextObjHandle)tokenBack.dataHandle;
  202.                     myDataHandle = (Handle)TEGetText((*textToken)->theText);
  203.                     HLock(myDataHandle);
  204.                     dataSize = GetHandleSize(myDataHandle);
  205.                     /* again, this check is not really necessary, but I want to put it here */
  206.                     /* as a warning, and maybe someday I'll stop using TextEdit. */
  207.                     if (dataSize > k64k) {
  208.                         /* if this were true, you would have to do something. */
  209.                         dataSize = k64k;
  210.                     }
  211.                     myErr = AECreateDesc(typeChar, (Ptr)*myDataHandle, dataSize, &returnDesc);
  212.                     HUnlock(myDataHandle);
  213.                     /* and kill the remaining token */
  214.                     myErr = MyDisposeTokenProc(&tokenBack);
  215.                     /* the descriptor will be added to the reply later in this function */
  216.                     break;
  217.                 case cWord:
  218.                     /* same basic thing for a word as for a text */
  219.                     wordToken = (CWordObjHandle)tokenBack.dataHandle;
  220.                     myDataHandle = NewHandle((*wordToken)->endPos - (*wordToken)->startPos);
  221.                     HLock(myDataHandle);
  222.                     dataSize = GetHandleSize(myDataHandle);
  223.                     /* move the text from the te chars handle to my data handle */
  224.                     tempHandle = (Handle)TEGetText((*wordToken)->theText);
  225.                     HLock(tempHandle);
  226.                     BlockMove((Ptr)(*tempHandle + (*wordToken)->startPos), (Ptr)*myDataHandle, dataSize);
  227.                     HUnlock(tempHandle);
  228.                     /* again, this check is not really necessary, but I want to put it here */
  229.                     /* as a warning. */
  230.                     if (dataSize > k64k) {
  231.                         /* if this were true, you would have to do something. */
  232.                         dataSize = k64k;
  233.                     }
  234.                     myErr = AECreateDesc(typeChar, (Ptr)*myDataHandle, dataSize, &returnDesc);
  235.                     HUnlock(myDataHandle);
  236.                     /* return value will be added to reply later in this function */
  237.                     /* and kill the remaining token */
  238.                     myErr = MyDisposeTokenProc(&tokenBack);
  239.                     
  240.                     break;
  241.                 case typeProperty:
  242.                     /* just return the property as the type included in the token */
  243.                     /* The SetData handler (as you will see) needs to do a lot more */
  244.                     propertyToken = (PropertyTHdl)tokenBack.dataHandle;
  245.                     HLock((Handle)propertyToken);
  246.                     myDataHandle = (*propertyToken)->theData;
  247.                     dataSize = GetHandleSize(myDataHandle);
  248.                     /* again, this check is not really necessary, but I want to put it here */
  249.                     /* as a warning. */
  250.                     if (dataSize > k64k) {
  251.                         /* if this were true, you would have to do something. */
  252.                         dataSize = k64k;
  253.                     }
  254.                     HLock(myDataHandle);
  255.                     myErr = AECreateDesc((*propertyToken)->theDataType, (Ptr)*myDataHandle, dataSize, &returnDesc);
  256.                     myErr = MyDisposeTokenProc(&tokenBack);
  257.                     break;
  258.                     /* and others as I add them */
  259.                 case cGraphicShape:
  260.                 case cGraphicLine:
  261.                     MakeGraphicLine((CShapeObjHandle)tokenBack.dataHandle, &returnDesc);
  262.                     myErr = MyDisposeTokenProc(&tokenBack);
  263.                     break;
  264.                 case cRectangle:
  265.                 case cOval:
  266.                     /* both of these have a default descriptor type of typeRectange, so I'll go make that */
  267.                     MakeTypeRect((CShapeObjHandle)tokenBack.dataHandle, &returnDesc);
  268.                     myErr = MyDisposeTokenProc(&tokenBack);
  269.                     break;
  270.                     
  271.             }
  272.             
  273.             /* Here I either have a good descriptor for a return value, or I have  */
  274.             /* 'null' in the type field if something went wrong. */
  275.             if (returnDesc.descriptorType == typeNull) {
  276.                 /* something bad happend, there will be no reply.   */
  277.                 /* the error is already in myErr */
  278.             } else {
  279.                 AEDesc newDesc;
  280.                 AEDesc wantType;
  281.                 OSErr tempErr;
  282.                 /* we have a good descriptor.  THere's one more thing to do, and it's */
  283.                 /* technically optional, but you should support it if you can.  That's  */
  284.                 /* seeing if the sender included a type that the wanted the data back as. */
  285.                 /* if they did, I will try and coerce my return descriptor to that type */
  286.                 /* First, is there a wantType? */
  287.                 /* I equate this to tempErr instead of myErr because it is not fatal if */
  288.                 /* this get fails, I don't want to pass this error back to the sender */
  289.  
  290.  
  291.                 tempErr = AEGetKeyDesc(messagein, keyAERequestedType, typeType, &wantType);
  292.                 if (!tempErr) {
  293.                     /* there was a wantType.  Try and coerce my data to that type */
  294.                     /* If the coercion fails, I will just return my type of data.  The  */
  295.                     /* sender of this event may be able to do the coercion on their */
  296.                     /* end, perhaps they have more coercion handlers on their machine */
  297.                     /* • NOTE: DON'T assume that you can't coerce the descriptor! */
  298.                     /* i.e. don't  evaluate the wantType yourself and say */
  299.                     /* "my application doesn't know how to do that" and fail. */
  300.                     /* True, your app may not know.  But someone else on the */
  301.                     /* machine MIGHT know, and they may have installed a system */
  302.                     /* level coercion routine that will make this work even if */
  303.                     /* your application knew nothing about it.  So try the coercion */
  304.                     tempErr = AECoerceDesc(&returnDesc, (DescType)**(wantType.dataHandle), &newDesc);
  305.                     if (!tempErr) {
  306.                         AEDesc tempDesc;
  307.                         /* it coerced.  So, because of the way I'm doing this, swap */
  308.                         /* things around (kinda) */
  309.                         tempDesc = returnDesc;
  310.                         returnDesc = newDesc;
  311.                         AEDisposeDesc(&tempDesc);
  312.                     }
  313.                 }
  314.  
  315.                 myErr = AEPutParamDesc(reply, keyDirectObject, &returnDesc);
  316.                 myErr = AEDisposeDesc(&returnDesc);
  317.                 
  318.             }
  319.         }                                                   /* aeresolve error */
  320.     }                                                       /* get object */
  321.     gCurrentReply = nil;
  322.     return(myErr);
  323.     
  324. }
  325.  
  326. pascal OSErr AESetDataHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  327. {
  328. #pragma unused (refIn)
  329.     OSErr myErr = noErr;
  330.     PropertyTHdl propertyBack;
  331.     CTextObjHandle cTextBack;
  332.     CWordObjHandle cWordBack;
  333.     AEDesc tokenBack;
  334.     AEDesc theObject;
  335.     AEDesc theData;
  336.     gCurrentReply = reply;
  337.     mVerboseOutput("\p\nEntering SetData handler")
  338.     myErr = AEGetKeyDesc(messagein, keyDirectObject, typeObjectSpecifier, &theObject);
  339.     
  340.     mAEErrorDisplay("\p getting object spec", myErr)
  341.     /* get the data to set now also.  If we don't have any data, we can't */
  342.     /* do this event. */
  343.     /* I'm asking for the data as typeWildCard, so no coercions will take place. */
  344.     /* I may coerce the data later, but now I want it as sent. */
  345.     myErr = AEGetKeyDesc(messagein, keyAEData, typeWildCard, &theData);
  346.         mAEErrorDisplay("\p getting the data", myErr)
  347.     myErr = AEResolve(&theObject, kAEIDoMinimum, &tokenBack);
  348.         mAEErrorDisplay("\p AEResolve", myErr)
  349.     if (!myErr) {
  350.         /* we have a token telling us what the object to set is.  */
  351.         /* from that, we can do our setting */
  352.         /* How you actually implement this is again very much up to  */
  353.         /* you. This is only a simple sample, do what seems best in your application */
  354.         switch (tokenBack.descriptorType) {
  355.             case cWindow:
  356.                 /* this shouldn't ever come back, since you can't set a  */
  357.                 /* window per sé.  Look at the property switch for that stuff */
  358.                 break;
  359.             case cText:
  360.                 /* setting the text of this text token */
  361.                 /* our text token contains a TEHandle, so here's what we'll do.... */
  362.                 cTextBack = (CTextObjHandle)tokenBack.dataHandle;
  363.                 /* select all the text */
  364.                 TESetSelect(0, 32000, (*cTextBack)->theText);
  365.                 /* kill the old text */
  366.                 TEDelete((*cTextBack)->theText);
  367.                 HLock(theData.dataHandle);
  368.                 /* and set the new text */
  369.                 TESetText((Ptr)*(theData.dataHandle), GetHandleSize((theData.dataHandle)), (*cTextBack)->theText);
  370.                 HUnlock(theData.dataHandle);
  371.                 /* fìn */
  372.                 MyDisposeTokenProc(&tokenBack);
  373.                 break;
  374.             case cWord:
  375.                 /* changing this one word to something else */
  376.                 /* basically the same as setting text, our range is just delimited */
  377.                 /* setting the text of this text token */
  378.                 /* our text token contains a TEHandle, so here's what we'll do.... */
  379.                 cWordBack = (CWordObjHandle)tokenBack.dataHandle;
  380.                 /* select all the text */
  381.                 TESetSelect((*cWordBack)->startPos, (*cWordBack)->endPos, (*cWordBack)->theText);
  382.                 TEDelete((*cWordBack)->theText);
  383.                 HLock(theData.dataHandle);
  384.                 TEInsert((Ptr)*(theData.dataHandle), GetHandleSize((theData.dataHandle)), (*cWordBack)->theText);
  385.                 /* fìn */
  386.                 MyDisposeTokenProc(&tokenBack);
  387.                 break;
  388.             case typeProperty:
  389.                 propertyBack = (PropertyTHdl)tokenBack.dataHandle;
  390.                 /* since there are so many properties to set, I'll switch off  */
  391.                 /* the owner and set to that type */
  392.                 switch ((*propertyBack)->owningTokenType) {
  393.                     case cWindow:
  394.                         myErr = SetWindowProperty(propertyBack, &theData);      /* in Windows.c */
  395.                         MyDisposeTokenProc(&tokenBack);
  396.                         break;
  397.                     case cText:
  398.                     case cWord:
  399.                     /* I'm not setting text or word properties in this sample, since I'm */
  400.                     /* using plain old TextEdit (not even style) */
  401.                         myErr = errAENotModifiable;
  402.                         AddToReply("\p Text property setting not implemented",0);
  403.                         break;
  404.                     case cGraphicLine:
  405.                     case cRectangle:
  406.                     case cOval:
  407.                         myErr = SetShapeProperty(propertyBack, &theData);      /* in Windows.c */
  408.                         MyDisposeTokenProc(&tokenBack);
  409.                     break;
  410.                     
  411.                     case typeProperty:
  412.                         /* uhhh, if you ever have to set the property of a proprty, call me */
  413.                         /* and we'll find help */
  414.                         break;
  415.                         
  416.                 }
  417.                 break;
  418.         }
  419.     }
  420.     gCurrentReply = nil;
  421.     return(myErr);
  422. }
  423.  
  424. /* TextFromWindow returns a token identifying a text object attached to a window. */
  425. /* If there is no text in this window, it returns an error */
  426. pascal OSErr TextFromWindow(DescType desiredClass, const AEDesc *container, DescType containerClass, DescType form,
  427.                             const AEDesc *selectionData, AEDesc *value, long LongInt)
  428. {
  429. #pragma unused (desiredClass,form,LongInt)
  430.     OSErr myErr = noErr;
  431.     WindowPtr theWindow;
  432.     windowCHandle tempWC;
  433.     short textNumber;
  434.     CTextObjHandle newText;
  435.     
  436.     mVerboseOutput("\p\nGetting a Text from a Window")
  437.     if (containerClass != cWindow) {
  438.         myErr = errAENoSuchObject;                          /* no such luck speedy */
  439.         AddToReply("\pYou did not ask for a text from a window, it's container", 0);
  440.     } else {
  441.         /* the container contains a window token in it's hot little datahandle */
  442.         theWindow = *((WindowPtr *)(*(container->dataHandle)));
  443.         
  444.         tempWC = (windowCHandle)GetWRefCon(theWindow);
  445.         /* Does this window contain a text edit record???? */
  446.         if ((*tempWC)->boxHandle) {
  447.             /* which number Text did they ask for?  Our sample only allows one */
  448.             /* Text per window, so if it isn't one, bail */
  449.             ShortFromDesc(&textNumber, selectionData);
  450.             if (textNumber == 1) {
  451.                 /* create an cText object and pass it back */
  452.                 newText = (CTextObjHandle)NewHandle(sizeof(CTextObject));
  453.                 HLock((Handle)newText);
  454.                 /* fill it in */
  455.                 (*newText)->theOwningWindow = theWindow;
  456.                 (*newText)->theText = (*tempWC)->boxHandle;
  457.                 myErr = AECreateDesc(cText, (Ptr)*newText, sizeof(CTextObject), value);
  458.                 DisposHandle((Handle)newText);              /* no longer needed */
  459.             } else {
  460.                 myErr = errAENoSuchObject;
  461.                 AddToReply("\pNo Text Box with that index", 0);
  462.             }
  463.         } else {
  464.             /* no text in this window */
  465.             myErr = errAENoSuchObject;
  466.             AddToReply("\pNo Text Box in the window you specified", 0);
  467.         }
  468.     }
  469.     return(myErr);
  470. }
  471.  
  472. /* Grab a word from the text */
  473. /* This is a 3rd level call from AEResolve.  Before we got to here, AEResolve has  */
  474. /* already called our WindFromNull and TextFromWindow accessors, the container */
  475. /* passed in is our text container token (probably) */
  476. pascal OSErr WordFromText(DescType desiredClass, const AEDesc *container, DescType containerClass, DescType form,
  477.                           const AEDesc *selectionData, AEDesc *value, long LongInt)
  478. {
  479.  #pragma unused (desiredClass,containerClass,form,LongInt)
  480.     OSErr myErr = noErr;
  481.     long wordCount;
  482.     long wordStart;
  483.     long wordEnd;
  484.     short countToGet;
  485.     SignedByte textHState;
  486.     CWordObjHandle wordToken;
  487.     Handle theText;
  488.     /* Since we'e getting word from text, we know the container is our Text token */
  489.     CTextObjHandle ourTextObject = (CTextObjHandle)(container->dataHandle);
  490.     /* grab the text for later */
  491.     theText = (Handle)TEGetText((*ourTextObject)->theText);
  492.     textHState = HGetState(theText);
  493.     HLock(theText);
  494.     if (GetHandleSize(theText)) {
  495.         mVerboseOutput("\p\nGetting a word from a Text")
  496.         ShortFromDesc(&countToGet, selectionData);
  497.         
  498.         /* call my word counter with a stopAt of the selection data */
  499.         CountWords(container, &wordCount, countToGet, &wordStart);
  500.         /* CountWords returns negative if there weren't any, or if we went too far */
  501.         if (wordCount > 0) {
  502.             /* now count to the next word, so we know the range */
  503.             /* I know, I know, this is horribly inefficient, but it's an example, OK? */
  504.             CountWords(container, &wordCount, countToGet + 1, &wordEnd);
  505.             if (wordCount < 0) {
  506.                 /* ooops, no more words.  Just go to text len then */
  507.                 wordEnd = GetHandleSize(theText);
  508.             }
  509.             /* return a word token */
  510.             wordToken = (CWordObjHandle)NewHandleClear(sizeof(CWordObject));
  511.             /* fill in the token */
  512.             (*wordToken)->theOwningWindow = (*ourTextObject)->theOwningWindow;
  513.             (*wordToken)->theText = (*ourTextObject)->theText;
  514.             (*wordToken)->startPos = wordStart;
  515.             (*wordToken)->endPos = wordEnd;
  516.             /* create the token */
  517.             myErr = AECreateDesc(cWord, (Ptr)*wordToken, sizeof(CWordObject), value);
  518.         } else {
  519.             /* error, no words */
  520.             myErr = errAENoSuchObject;
  521.             AddToReply("\pCould not find this number word in this text", 0);
  522.         }
  523.     } else {
  524.         myErr = errAENoSuchObject;
  525.         AddToReply("\pNo words in this Text", 0);
  526.     }
  527.     HSetState(theText, textHState);
  528.     return(myErr);
  529. }
  530.  
  531. pascal OSErr DrawFromWindow(DescType desiredClass, const AEDesc *container, DescType containerClass, DescType form,
  532.                             const AEDesc *selectionData, AEDesc *value, long LongInt)
  533. {
  534. #pragma unused (desiredClass,containerClass,form,selectionData,LongInt)
  535.     OSErr myErr = noErr;
  536.     mVerboseOutput("\p\nGetting a Drawing Area from a window")
  537.     /* the drawing area token is the same as a window token (i.e. a window pointer) since it gives */
  538.     /* us everything we need to know */
  539.     /* so basically we duplicate the descriptor passed in to us, then change the descType */
  540.     myErr = AEDuplicateDesc(container, value);
  541.     value->descriptorType = cDrawingArea;
  542.     return(myErr);
  543. }
  544.  
  545. /* ShapeFromDraw pulls _any_ kind of shape from my drawing area.  It's called for */
  546. /* a generic cGraphicShape, a cGraphicLine,cRectangle, and cOval.  This is where */
  547. /* your code gets optimized and shrunken, so it is not the nightmare you  */
  548. /* at first thought.  Really */
  549. /* Note that this also gets called for shapes from cWindow (if someone asks that way) */
  550. /* so golly, it's just so useful */
  551. pascal OSErr ShapeFromDraw(DescType desiredClass, const AEDesc *container, DescType containerClass, DescType form,
  552.                            const AEDesc *selectionData, AEDesc *value, long LongInt)
  553. {
  554.  #pragma unused (LongInt)
  555.     OSErr myErr = noErr;
  556.     windowCHandle tempWC;
  557.     long selNumber;
  558.     long tempCounter;
  559.     CShapeObjHandle theToken = (CShapeObjHandle)NewHandleClear(sizeof(CShapeObject));
  560.     ShapesHandle theShapes;
  561.     short numShapes;
  562.     mVerboseOutput("\p\nGetting a Shape from a window, specifically a ")
  563.     AddToAEWindow((Ptr)&desiredClass, kFour);
  564.     /* the container may be either a cWindow or a cDrawingArea.  In our infinite wisdom, */
  565.     /* we have made these tokens the same, so we can use the same routine for both. */
  566.     if (containerClass == cWindow || containerClass == cDrawingArea) {
  567.         /* get the right window struct and the shapes attached to it */
  568.         tempWC = (windowCHandle)GetWRefCon(*((WindowPtr *)(*(container->dataHandle))));
  569.         theShapes = (*tempWC)->theShapes;
  570.         numShapes = (*tempWC)->numShapes;
  571.         /* do some default filling of the shape token, since there's some things */
  572.         /* we already know */
  573.         (*theToken)->theOwningWindow = *((WindowPtr *)(*(container->dataHandle)));
  574.         (*theToken)->type = typeNull;                          /* flag for later folks */
  575.         if (numShapes) {
  576.             /* case off the type of shape we're asking for for the specific one */
  577.             switch (desiredClass) {
  578.                 case cGraphicShape:
  579.                     /* just any old thing */
  580.                     /* case off the form wanted.  Since I am only sending myself graphics */
  581.                     /* by index, that's the case I'll do first */
  582.                 case cGraphicLine:
  583.                 case cRectangle:
  584.                 case cOval:
  585.                     /* and all these shapes are basically the same, so I'll use the same routine */
  586.                     /* in fact, I'm going to go back to my window routine and make the type filed in my Shapes */
  587.                     /* handle a desc type, hold on a minute....  OK, I'm back, this will make it even easier */
  588.                     switch (form) {
  589.                         case formAbsolutePosition:
  590.                             if (selectionData->descriptorType == typeLongInteger) {
  591.                                 
  592.                                 selNumber = *((long *)*(selectionData->dataHandle));
  593.                             } else {
  594.                                 /* see if you can coerce it to a long */
  595.                             }
  596.                             /* find the selNumber-th line in this drawing area */
  597.                             if (selNumber == 0)
  598.                                 selNumber = 1;              /* no 0th */
  599.                             tempCounter = 0;
  600.                             while (theShapes) {
  601.                                 /* 'if' here depending on generic shape or specific type */
  602.                                 if (desiredClass == cGraphicShape) {
  603.                                     tempCounter++;          /* doesn't matter what kind, we care about it */
  604.                                 } else {
  605.                                     if ((*theShapes)->aeType == desiredClass)
  606.                                         tempCounter++;
  607.                                 }
  608.                                 if (tempCounter == selNumber) {
  609.                                     break;
  610.                                 } else {
  611.                                     theShapes = (*theShapes)->nextShape;
  612.                                 }
  613.                             }
  614.                             /* if theShapes is not nil, then we found a shape that suites */
  615.                             
  616.                             if (theShapes) {
  617.                                 /* fill in my token */
  618.                                 (*theToken)->theShape = theShapes;
  619.                                 (*theToken)->shapeNumber = tempCounter;     /* why do I want this?  I dunno */
  620.                                 (*theToken)->type = desiredClass;
  621.                             } else {
  622.                                 myErr = errAENoSuchObject;
  623.                             }
  624.                             break;
  625.                         case formRelativePosition:          /* next, previous */
  626.                             break;
  627.                         case formTest:                      /* A logical or a comparison */
  628.                             break;
  629.                         case formRange:                     /* Two arbitrary objects and everything in between */
  630.                             break;
  631.                         case formPropertyID:                /* Key data is a 4-char property name */
  632.                             break;
  633.                         case formName:                      /* Key data may be of type 'TEXT' */
  634.                             
  635.                             break;
  636.                         default:
  637.                             myErr = errAENoSuchObject;
  638.                             break;
  639.                     }
  640.                     break;
  641.             }
  642.         } else {
  643.             /* no shapes with this window */
  644.             myErr = errAENoSuchObject;
  645.         }
  646.     } else {
  647.         myErr = errAENoSuchObject;
  648.     }
  649.     if (myErr == noErr && (*theToken)->type != typeNull) {
  650.         HLock((Handle)theToken);
  651.         myErr = AECreateDesc((*theToken)->type, (Ptr)*theToken, sizeof(CShapeObject), value);
  652.         DisposHandle((Handle)theToken);
  653.     }
  654.     return(myErr);
  655. }
  656.  
  657. /* In this example, this will be the first accessor called by any call to AEResolve. */
  658. /* Since we have no other objects sprouting from null but windows, anyone who */
  659. /* wants to know about our documents has to have a cWindow object spec hanging off the */
  660. /* topmost null. */
  661. pascal OSErr WindFromNull(DescType desiredClass, const AEDesc *container, DescType containerClass, DescType form,
  662.                           const AEDesc *selectionData, AEDesc *value, long LongInt)
  663. {
  664.  #pragma unused (desiredClass,container,LongInt)
  665.     WindowPtr returnedWindow = nil;
  666.     WindowPtr tempWindow = nil;
  667.     short index = 0;
  668.     short counter = 0;
  669.     OSErr myErr = noErr;
  670.     Str63 tempMatch;
  671.     Str63 windowName;
  672.     
  673.     mVerboseOutput("\p\nGetting a Window from Nul")
  674.     /* first check to see if we can deal with this, are they asking for a window from Nul?  */
  675.     /* If they're not then we can't handle it */
  676.     if (containerClass != typeNull)
  677.         return(errAENoSuchObject);                          /* not something we recognize */
  678.     /* case off form to see how we'll get the window thing */
  679.     /* all of these may not be supported, but as time goes by they will be */
  680.     
  681.     switch (form) {
  682.         case formAbsolutePosition:                          /* e.g., 1st, -2nd ( 2nd from end) */
  683.             /* ignore the AEStatusWindow in searching this list */
  684.             tempWindow = FrontWindow();
  685.             /* a little utilty routine, see which number window they want */
  686.             ShortFromDesc(&index, (AppleEvent *)selectionData);
  687.             if (index == 0)
  688.                 index = 1;
  689.             if (index == 1 && (((WindowPeek)FrontWindow())->windowKind != kAEStatusWindow)) {
  690.                 /* just grab the front window and return */
  691.                 returnedWindow = FrontWindow();
  692.             } else {
  693.                 /* walk the list  */
  694.                 tempWindow = mWindList;
  695.                 while (tempWindow) {
  696.                     windowCHandle tempWC = (windowCHandle)GetWRefCon(tempWindow);
  697.                     if (index == (*tempWC)->windowIndex) {
  698.                         returnedWindow = tempWindow;
  699.                         break;
  700.                     }
  701.                     tempWindow = (WindowPtr)((WindowPeek)tempWindow)->nextWindow;
  702.                 }
  703.             }
  704.             if (returnedWindow) {
  705.                 
  706.                 myErr = AECreateDesc(cWindow, (Ptr)&returnedWindow, sizeof(returnedWindow), value);
  707.                 mAEErrorDisplay("\p creating object token", myErr);
  708.             } else {
  709.                 /* error, couldn't find the thing */
  710.                 myErr = errAENoSuchObject;
  711.                 AddToReply("\pNo window of that index number", 0);
  712.             }
  713.             break;
  714.         case formRelativePosition:                          /* next, previous */
  715.             break;
  716.         case formTest:                                      /* A logical or a comparison */
  717.             break;
  718.         case formRange:                                     /* Two arbitrary objects and everything in between */
  719.             break;
  720.         case formPropertyID:                                /* Key data is a 4-char property name */
  721.             
  722.             break;
  723.         case formName:                                      /* Key data may be of type 'TEXT' */
  724.             /* gettting one of these here things by name */
  725.             /* so the AEDesc has a text thing in it.  Not a PString, just a charater thing, */
  726.             /* so I'll make it a PString */
  727.             PStringFromTextDesc(&tempMatch, selectionData);
  728.             /* and as usual, walk the window list */
  729.             tempWindow = FrontWindow();
  730.             while (tempWindow) {
  731.                 GetWTitle(tempWindow, windowName);
  732.                 if (EqualString(windowName, tempMatch, false, false))
  733.                     break;
  734.                 tempWindow = (WindowPtr)((WindowPeek)tempWindow)->nextWindow;
  735.             }
  736.             if (tempWindow) {
  737.                 /* we have a match */
  738.                 myErr = AECreateDesc(cWindow, (Ptr)&tempWindow, sizeof(tempWindow), value);
  739.                 mAEErrorDisplay("\p creating object token", myErr);
  740.             } else {
  741.                 /* no match, return an error  */
  742.                 myErr = errAENoSuchObject;
  743.                 AddToReply("\pNo window with that name", 0);
  744.             }
  745.             break;
  746.             
  747.     }
  748.     return(myErr);
  749. }
  750.  
  751. /* PropertyFromWindow gets all the properties of my window object. */
  752. pascal OSErr PropertyFromWindow(DescType desiredClass, const AEDesc *container, DescType containerClass, DescType form,
  753.                                 const AEDesc *selectionData, AEDesc *value, long LongInt)
  754. {
  755.  #pragma unused (desiredClass,containerClass,form,LongInt)
  756.  
  757.     OSErr myErr = noErr;
  758.     Str63 windowName;
  759.     WindowPtr tempPort;
  760.     PropertyTHdl propertyTokenH = (PropertyTHdl)NewHandleClear(sizeof(PropertyToken));
  761.     /* pull a property from the window */
  762.     DescType propertyType = *(DescType *)(*(selectionData->dataHandle));
  763.     ProcessSerialNumber tempPSN;
  764.     Boolean tempBool;
  765.     Handle theData;
  766.     WindowPtr owningWindow;
  767.     windowCHandle owningControl;
  768.     mVerboseOutput("\p\n Getting a window property ")
  769.     AddToAEWindow((Ptr)&propertyType, kFour);
  770.     /* I can fill in some of the token information right now, since I have the window */
  771.     owningWindow = *((WindowPtr *)(*(container->dataHandle)));
  772.     owningControl = (windowCHandle)GetWRefCon(owningWindow);
  773.     (*propertyTokenH)->owningTokenType = cWindow;           /* what this is a property of */
  774.     (*propertyTokenH)->token.window = owningWindow;
  775.     (*propertyTokenH)->inWindow = owningWindow;    /* for consistency, though it's redundant */
  776.     switch (propertyType) {
  777.         /* can't get all of them yet, as this sample grows the amount of properties will also */
  778.         /* grow */
  779.         
  780.         /* What's the name of this window? */
  781.         case pName:
  782.             (*propertyTokenH)->theProperty = pName;
  783.             (*propertyTokenH)->theDataType = typeChar;
  784.             GetWTitle(owningWindow, windowName);
  785.             theData = NewHandle(windowName[0]);
  786.             HLock(theData);
  787.             BlockMove((Ptr)&windowName[1], (Ptr)*theData, windowName[0]);
  788.             HUnlock(theData);
  789.             (*propertyTokenH)->theData = theData;
  790.             break;
  791.             /* Boundry rectangle, in global coordinates */
  792.         case pBounds:
  793.             (*propertyTokenH)->theProperty = pBounds;
  794.             (*propertyTokenH)->theDataType = typeQDRectangle;
  795.             theData = NewHandle(sizeof(Rect));
  796.             HLock(theData);
  797.             BlockMove((Ptr)&owningWindow->portRect, (Ptr)*theData, sizeof(Rect));
  798.             /* now we have to globalize the coordiantes */
  799.             GetPort(&tempPort);
  800.             SetPort(owningWindow);
  801.             SetPort(tempPort);
  802.             /* Now, can we force the MPW C compiler to beleive us that what we're passing is  */
  803.             /* a Point pointer???? */
  804.             LocalToGlobal((Point *)*theData);
  805.             LocalToGlobal((Point *)(*theData + sizeof(Point)));
  806.             /* yes we can, amazing */
  807.             
  808.             HUnlock(theData);
  809.             (*propertyTokenH)->theData = theData;
  810.             
  811.             break;
  812.             /* Do we have a title bar? */
  813.         case pHasTitleBar:
  814.             (*propertyTokenH)->theProperty = pHasTitleBar;
  815.             (*propertyTokenH)->theDataType = typeBoolean;
  816.             theData = NewHandle(sizeof(short));
  817.             **theData = kMyTrue;                            /* they all do */
  818.             (*propertyTokenH)->theData = theData;
  819.             break;
  820.             /* are we a modal window? */
  821.         case pIsModal:
  822.             (*propertyTokenH)->theProperty = pIsModal;
  823.             (*propertyTokenH)->theDataType = typeBoolean;
  824.             theData = NewHandle(sizeof(short));
  825.             **theData = ((WindowPeek)owningWindow)->windowKind & dialogKind;
  826.             (*propertyTokenH)->theData = theData;
  827.             break;
  828.             /* is this window 'dirty'? */
  829.         case pIsModified:
  830.             (*propertyTokenH)->theProperty = pIsModified;
  831.             (*propertyTokenH)->theDataType = typeBoolean;
  832.             theData = NewHandle(sizeof(short));
  833.             **theData = (*owningControl)->windowDirty;
  834.             (*propertyTokenH)->theData = theData;
  835.             break;
  836.             /* can the size be changed? */
  837.         case pIsResizable:
  838.             (*propertyTokenH)->theProperty = pIsResizable;
  839.             (*propertyTokenH)->theDataType = typeBoolean;
  840.             theData = NewHandle(sizeof(short));
  841.             **theData = kMyTrue;
  842.             (*propertyTokenH)->theData = theData;
  843.             break;
  844.             /* stationary? */
  845.         case pIsStationeryPad:
  846.             (*propertyTokenH)->theProperty = pIsResizable;
  847.             (*propertyTokenH)->theDataType = typeBoolean;
  848.             theData = NewHandle(sizeof(short));
  849.             **theData = false;
  850.             (*propertyTokenH)->theData = theData;
  851.             break;
  852.             /* Are we in the users face right now? */
  853.         case pIsFrontProcess:
  854.             (*propertyTokenH)->theProperty = pIsResizable;
  855.             (*propertyTokenH)->theDataType = typeBoolean;
  856.             theData = NewHandle(sizeof(short));
  857.             GetFrontProcess(&tempPSN);
  858.             SameProcess(&tempPSN, &gOurSN, &tempBool);
  859.             **theData = tempBool;
  860.             (*propertyTokenH)->theData = theData;
  861.             break;
  862.             /* are we in the Zoomed state? */
  863.         case pIsZoomed:
  864.             break;
  865.             /* are we visible to the user now? */
  866.         case pVisible:
  867.             (*propertyTokenH)->theProperty = pVisible;
  868.             (*propertyTokenH)->theDataType = typeBoolean;
  869.             theData = NewHandle(sizeof(short));
  870.             **theData = kMyTrue;                            /* I'm only returning visible windows  */
  871.             (*propertyTokenH)->theData = theData;
  872.             break;
  873.             /* What's our class? */
  874.         case pClass:
  875.             (*propertyTokenH)->theProperty = pClass;
  876.             (*propertyTokenH)->theDataType = typeType;
  877.             
  878.             theData = NewHandle(sizeof(DescType));
  879.             HLock(theData);
  880.             *((DescType *)*theData)=cWindow;
  881.             HUnlock(theData);
  882.             (*propertyTokenH)->theData = theData;
  883.             
  884.             break;
  885.             
  886.     }
  887.     
  888.     /* and return the token */
  889.     if (!myErr) {
  890.         HLock((Handle)propertyTokenH);
  891.         myErr = AECreateDesc(typeProperty, (Ptr)*propertyTokenH, sizeof(PropertyToken), value);
  892.         /* dispose of the token handle, but NOT the data handle I added, since the AEM */
  893.         /* had no idea that I put a handle inside the data, so it didn't copy that */
  894.         /* Later, when the token itself is disposed that handle will be  */
  895.         /* freed up. */
  896.         DisposHandle((Handle)propertyTokenH);
  897.         mAEErrorDisplay("\p creating property token", myErr);
  898.         
  899.     }
  900.     return(myErr);
  901. }
  902. /* This allows someone to get properties from my text */
  903. pascal OSErr PropertyFromText(DescType desiredClass, const AEDesc *container, DescType containerClass, DescType form,
  904.                               const AEDesc *selectionData, AEDesc *value, long LongInt)
  905. {
  906. #pragma unused (desiredClass,containerClass,form,value,LongInt)
  907.  
  908.     OSErr myErr = noErr;
  909.     PropertyTHdl propertyTokenH = (PropertyTHdl)NewHandleClear(sizeof(PropertyToken));
  910.     CTextObjHandle myTextHandle;                            /* I include a text object token in my text property token */
  911.     /* kinda circular, but hey... */
  912.     /* pull a property from a text */
  913.     
  914.     DescType propertyType = *(DescType *)(*(selectionData->dataHandle));
  915.     Handle theData;
  916.     RGBColor myBlack =  {
  917.         0, 0, 0
  918.     };                                                      /* for the Color question */
  919.     Str255 tempString;
  920.     windowCHandle owningControl;
  921.     mVerboseOutput("\p\n Getting a text property ")
  922.     AddToAEWindow((Ptr)&propertyType, kFour);
  923.     /* I can fill in some of the token information right now, since I have the window */
  924.     myTextHandle = (CTextObjHandle)container->dataHandle;
  925.     owningControl = (windowCHandle)GetWRefCon((*myTextHandle)->theOwningWindow);
  926.     (*propertyTokenH)->owningTokenType = cText;             /* what this is a property of */
  927.     (*propertyTokenH)->token.textHandle = (*myTextHandle)->theText;
  928.     (*propertyTokenH)->theProperty = propertyType;
  929.     (*propertyTokenH)->inWindow = (*myTextHandle)->theOwningWindow;
  930.     switch (propertyType) {WritingCodePtr theCode;
  931.         
  932.         case pAnyCharTextStyles:
  933.             (*propertyTokenH)->theDataType = typeBoolean;
  934.             theData = NewHandle(sizeof(short));
  935.             **theData = false;                              /* we have no style */
  936.             (*propertyTokenH)->theData = theData;
  937.             break;
  938.         case pClass:
  939.             (*propertyTokenH)->theDataType = typeType;
  940.             theData = NewHandle(sizeof(DescType));
  941.             **theData = cText;                              /* they all do */
  942.             (*propertyTokenH)->theData = theData;
  943.             break;
  944.         case pColor:
  945.             (*propertyTokenH)->theDataType = typeRGBColor;
  946.             /* always black in this example */
  947.             theData = NewHandle(sizeof(RGBColor));
  948.             HLock(theData);
  949.             BlockMove((Ptr)&myBlack, (Ptr)*theData, sizeof(RGBColor));
  950.             HUnlock(theData);
  951.             (*propertyTokenH)->theData = theData;
  952.             break;
  953.         case pFont:
  954.             /* get the ID, translate to a name, and return as text */
  955.             /* our token contains a TERecord, which contains the font ID */
  956.             GetFontName((*(*propertyTokenH)->token.textHandle)->txFont, &tempString);
  957.             theData = NewHandle(tempString[0]);
  958.             HLock(theData);
  959.             BlockMove((Ptr)&tempString[1], (Ptr)*theData, tempString[0]);
  960.             HUnlock(theData);
  961.             (*propertyTokenH)->theData = theData;
  962.             
  963.             break;
  964.         case pPointSize:
  965.             /* Object Class ID: cFixed16 */
  966.             (*propertyTokenH)->theDataType = typeFixed;
  967.             theData = NewHandleClear(sizeof(long));
  968.             *((short *)*theData) = (*(*propertyTokenH)->token.textHandle)->txSize;        
  969.             (*propertyTokenH)->theData = theData;
  970.             
  971.         case pScriptTag:
  972.             (*propertyTokenH)->theDataType = typeIntlWritingCode;
  973.             theData = NewHandle(sizeof(WritingCode));
  974.             theCode = (WritingCodePtr) *theData;
  975.             HLock(theData);
  976.             theCode->theScriptCode = FontScript();
  977.             theCode->theLangCode = iuSystemCurLang;
  978.             HUnlock(theData);
  979.             (*propertyTokenH)->theData = theData;
  980.  
  981.             break;
  982.         case pTextStyles:
  983.             /* in my sample, no style */
  984.             (*propertyTokenH)->theDataType = typeBoolean;
  985.             theData = NewHandle(sizeof(short));
  986.             **theData = false;                              /* we have no style */
  987.             (*propertyTokenH)->theData = theData;
  988.             
  989.             break;
  990.             /*****************************/
  991.             
  992.     }                                                       /* endswitch */
  993.     
  994.     
  995.     return(myErr);
  996. }
  997.  
  998. pascal OSErr PropertyFromShape(DescType desiredClass, const AEDesc *container, DescType containerClass, DescType form,
  999.                                const AEDesc *selectionData, AEDesc *value, long LongInt)
  1000. {
  1001. #pragma unused (desiredClass,containerClass,form,LongInt)
  1002.     OSErr myErr = noErr;
  1003.     PropertyTHdl propertyTokenH = (PropertyTHdl)NewHandleClear(sizeof(PropertyToken));
  1004.     /* pull a property from the window */
  1005.     CShapeObjHandle theShapeToken;                          /* the token for this shape */
  1006.     RGBColor white =  {
  1007.         0xFFFF, 0xFFFF, 0xFFFF
  1008.     };
  1009.     ShapesHandle theShape;
  1010.     short penSize = 1;
  1011.     long myCopy = 0;
  1012.     DescType propertyType = *(DescType *)(*(selectionData->dataHandle));
  1013.     Handle theData;
  1014.     windowCHandle owningControl;
  1015.     DescType shapeType;
  1016.     mVerboseOutput("\p\n Getting a property from a shape")
  1017.     theShapeToken = (CShapeObjHandle)container->dataHandle;
  1018.     owningControl = (windowCHandle)GetWRefCon((*theShapeToken)->theOwningWindow);
  1019.     shapeType = (*theShapeToken)->type;
  1020.     theShape = (*theShapeToken)->theShape;
  1021.     (*propertyTokenH)->owningTokenType = shapeType;
  1022.     (*propertyTokenH)->token.shapeHandle = (*theShapeToken)->theShape;
  1023.     (*propertyTokenH)->theProperty = propertyType;
  1024.     (*propertyTokenH)->inWindow = (*theShapeToken)->theOwningWindow;
  1025.     /* so after filling in a bunch, all we need now is the data type and the data itself */
  1026.     switch (propertyType) {
  1027.         case pBounds:
  1028.         case pDefinitionRect:
  1029.             /* This is the same as the bounds rect in my example */
  1030.             
  1031.             /* return a bounds rect, pretty easy */
  1032.             (*propertyTokenH)->theDataType = typeQDRectangle;
  1033.             theData = NewHandle(sizeof(Rect));
  1034.             HLock(theData);
  1035.             BlockMove((Ptr)&(*theShape)->theRect, (Ptr)*theData, sizeof(Rect));
  1036.             /* this of course stays in local coordinates for the owning port */
  1037.             (*propertyTokenH)->theData;
  1038.             break;
  1039.         case pClass:
  1040.             (*propertyTokenH)->theDataType = typeType;
  1041.             theData = NewHandle(sizeof(DescType));
  1042.             HLock(theData);
  1043.             /* I know, blockmoving 4 bytes is silly.  */
  1044.             BlockMove((Ptr)&shapeType, (Ptr)*theData, sizeof(DescType));
  1045.             break;
  1046.         case pFillColor:
  1047.             (*propertyTokenH)->theDataType = typeRGBColor;
  1048.             /* always white in this example */
  1049.             theData = NewHandle(sizeof(RGBColor));
  1050.             HLock(theData);
  1051.             BlockMove((Ptr)&white, (Ptr)*theData, sizeof(RGBColor));
  1052.             HUnlock(theData);
  1053.             (*propertyTokenH)->theData = theData;
  1054.             
  1055.             break;
  1056.         case pFillPattern:
  1057.         case pPenPattern:
  1058.             break;
  1059.         case pPenColor:
  1060.             (*propertyTokenH)->theDataType = typeRGBColor;
  1061.             theData = NewHandle(sizeof(RGBColor));
  1062.             HLock(theData);
  1063.             BlockMove((Ptr)&(*theShape)->theColor, (Ptr)*theData, sizeof(RGBColor));
  1064.             HUnlock(theData);
  1065.             (*propertyTokenH)->theData = theData;
  1066.             
  1067.             break;
  1068.             
  1069.         case pPenWidth:
  1070.             (*propertyTokenH)->theDataType = typeShortInteger;
  1071.             /* always white in this example */
  1072.             theData = NewHandle(sizeof(short));
  1073.             HLock(theData);
  1074.             BlockMove((Ptr)&penSize, (Ptr)*theData, sizeof(short));
  1075.             break;
  1076.         case pTransferMode:
  1077.             (*propertyTokenH)->theDataType = typeEnumeration;
  1078.             /* always white in this example */
  1079.             theData = NewHandle(sizeof(long));
  1080.             HLock(theData);
  1081.             BlockMove((Ptr)&myCopy, (Ptr)*theData, sizeof(long));
  1082.             break;
  1083.     }
  1084.     HUnlock(theData);
  1085.     /* dangme I forgot to move what I just did into result..... */
  1086.     if (!myErr) {
  1087.         HLock((Handle)propertyTokenH);
  1088.         myErr = AECreateDesc(typeProperty, (Ptr)*propertyTokenH, sizeof(PropertyToken), value);
  1089.         /* dispose of the token handle, but NOT the data handle I added, since the AEM */
  1090.         /* had no idea that I put a handle inside the data, so it didn't copy that */
  1091.         /* Later, when the token itself is disposed that handle will be  */
  1092.         /* freed up. */
  1093.         DisposHandle((Handle)propertyTokenH);
  1094.         mAEErrorDisplay("\p creating property token", myErr);
  1095.         
  1096.     }
  1097.     
  1098.     return(myErr);
  1099. }
  1100.  
  1101.  
  1102. /* You may never need a dispose function.  If all you use for tokens are */
  1103. /* simple AEDescs, the OSL will do all the disposal for you. */
  1104. /* In my case, my tokens contain  handles, so I have to  */
  1105. /* do some disposal myself. */
  1106. pascal OSErr MyDisposeTokenProc(AEDesc *unneededToken)
  1107. {
  1108.     OSErr myErr = noErr;
  1109.     PropertyTHdl myProp;
  1110.     mVerboseOutput("\p\nDisposing a Token: ")
  1111.     AddToAEWindow((Ptr)&(unneededToken->descriptorType), kFour);
  1112.     /* most of the time I'll be doing a simple AEDisposeDesc.  The only time I need  */
  1113.     /* something more complex is when I'm disposing of a property descriptor, since */
  1114.     /* I have a handle inside that */
  1115.     switch (unneededToken->descriptorType) {
  1116.         /* these two are simple */
  1117.         case cWindow:
  1118.         case cText:
  1119.             myErr = AEDisposeDesc(unneededToken);
  1120.             break;
  1121.             /* these two have imbedded handles */
  1122.         case cWord:
  1123.             myErr = AEDisposeDesc(unneededToken);
  1124.             break;
  1125.         case typeProperty:
  1126.             myProp = (PropertyTHdl)unneededToken->dataHandle;
  1127.             DisposHandle((*myProp)->theData);
  1128.             myErr = AEDisposeDesc(unneededToken);
  1129.             
  1130.             break;
  1131.             
  1132.         default:
  1133.             /* I default to just disposing of the token, ne ces pa  */
  1134.             AEDisposeDesc(unneededToken);
  1135.             break;
  1136.     }
  1137.     return(myErr);
  1138. }
  1139.  
  1140. /* not yet implemented (and also not installed as a callback) */
  1141. pascal OSErr MyCompareProc(DescType oper, const AEDesc *obj1, const AEDesc *obj2, Boolean *result)
  1142. {
  1143. #pragma unused (oper,obj1,obj2,result)
  1144.     OSErr myErr = noErr;
  1145.     mVerboseOutput("\p\nComparing Objects")
  1146.     return(myErr);
  1147. }
  1148.  
  1149. pascal OSErr MyCountProc(DescType desiredType, DescType containerClass, const AEDesc *container, long *result)
  1150. {
  1151.     OSErr myErr = noErr;
  1152.     long counter = 0;
  1153.     WindowPtr windows;
  1154.     windowCHandle tempWC;
  1155.     switch (desiredType) {
  1156.         case cWindow:
  1157.             /* should be conting windows from nul */
  1158.             mVerboseOutput("\p\nCounting Windows")
  1159.             windows = mWindList;
  1160.             while (windows) {
  1161.                 counter++;
  1162.                 windows = (WindowPtr)((WindowPeek)windows)->nextWindow;
  1163.             }
  1164.             *result = counter;
  1165.             break;
  1166.         case kAEMyShape:
  1167.             mVerboseOutput("\p\nCounting Shapes")
  1168.             /* getting this from a specific container, i.e. a winder. */
  1169.             switch (containerClass) {
  1170.                 case cWindow:
  1171.                 case cDrawingArea:
  1172.                     /* we're OK so far */
  1173.                     /* get my window pointer from the container passed */
  1174.                     CountShapes(container,&counter,nil,nil,&desiredType);
  1175.                     *result = counter;
  1176.                     break;
  1177.                 case typeNull:
  1178.                     /* this would mean all the possible shapes  */
  1179.                     break;
  1180.             }
  1181.             break;
  1182.         case cText:
  1183.             mVerboseOutput("\p\nCounting Texts")
  1184.             switch (containerClass) {
  1185.                 /* I can count texts in a window and in Null (the app) */
  1186.                 case typeNull:
  1187.                     /* walk all my document windows and see how many texts exisit */
  1188.                     windows = FrontWindow();
  1189.                     /* there may not be any, o'course */
  1190.                     while (windows) {
  1191.                         tempWC = (windowCHandle)GetWRefCon(windows);
  1192.                         if ((*tempWC)->boxHandle)
  1193.                             counter++;
  1194.                         windows = (WindowPtr)((WindowPeek)windows)->nextWindow;
  1195.                     }
  1196.                     *result = counter;
  1197.                     break;
  1198.                 case cWindow:
  1199.                     /* so we know the container desc is my window token */
  1200.                     windows = (WindowPtr)*(container->dataHandle);
  1201.                     tempWC = (windowCHandle)GetWRefCon(windows);
  1202.                     /* there is either one or none */
  1203.                     *result = (((*tempWC)->boxHandle) ? 1 : 0);
  1204.                     break;
  1205.                 default:
  1206.                     mVerboseOutput("\p\nAsked for text count from something we don't understand ")
  1207.                     myErr = errAECantHandleClass;
  1208.                             /* add new Winter '92 registry error code <ckh 1.0.2>*/
  1209.                     AddToReply("\p Cant count this class",errAECantHandleClass);
  1210.  
  1211.                     counter = 0;
  1212.                     break;
  1213.             }
  1214.             *result = counter;
  1215.             break;
  1216.         case cWord:
  1217.             /* Since I have to count words in another place (when I get the word) */
  1218.             /* I will call my subroutine, saying I don't want to stop */
  1219.             mVerboseOutput("\p\nCounting Words ")
  1220.             CountWords(container, &counter, nil, nil);
  1221.             *result = counter;
  1222.             break;
  1223.         case cGraphicLine:
  1224.         case cRectangle:
  1225.         case cOval:
  1226.         case cGraphicShape:
  1227.             /* all these are basically the same */
  1228.             CountShapes(container, &counter, nil, nil, &desiredType);
  1229.             *result = counter;
  1230.             break;
  1231.         default:
  1232.             myErr = errAENoSuchObject;                      /* I don't know what's being asked for */
  1233.             break;
  1234.  
  1235.     }
  1236.     return(myErr);
  1237. }
  1238.  
  1239. void CountWords(const AEDesc *container, long *theCount, long stopAt, long *position)
  1240. {
  1241.     CTextObjHandle ourTextObject;
  1242.     Handle theText;
  1243.     SignedByte textState;
  1244.     char *textPtr;
  1245.     register char letter;
  1246.     long wordCount = 1;                                     /* I'm getting passed a 1 based count to find */
  1247.     Boolean inSpaces = false;
  1248.     register long positionNow = 0;
  1249.     long lastLetter = 0;
  1250.     /* So we have a text object here, which contains a TEHandle */
  1251.     /* We step through the handle counting words */
  1252.     /* get the text from my cText token */
  1253.     
  1254.     AddAENum(stopAt);
  1255.     ourTextObject = (CTextObjHandle)(container->dataHandle);
  1256.     *position = -1;
  1257.     theText = (Handle)TEGetText((*ourTextObject)->theText);
  1258.     textState = HGetState(theText);
  1259.     HLock(theText);                                         /* AddAENum makes some TextEdit calls, so I need to lock this */
  1260.     /* how many characters are in it? */
  1261.     lastLetter = GetHandleSize(theText);
  1262.     if (lastLetter) {
  1263.         /* now this is a slow and agonizing word count routine, it's not intended to be any more than that */
  1264.         textPtr = (char *)*theText;
  1265.         for (positionNow = 0; positionNow < lastLetter; positionNow++) {
  1266.             letter = *(textPtr + positionNow);
  1267.             if (letter == kSpace) {
  1268.                 if (!inSpaces)
  1269.                     wordCount++;
  1270.                 inSpaces = kMyTrue;
  1271.                 if (stopAt && stopAt == wordCount) {
  1272.                     if (position)
  1273.                         *position = positionNow;
  1274.                     break;
  1275.                 }
  1276.             } else {
  1277.                 inSpaces = false;
  1278.             }
  1279.         }
  1280.         /* Did we have fun counting words?  Yes? */
  1281.         /* OK, we have a word count */
  1282.         /* if position is still negative, we didn't get to this word.  Maybe there weren't enough */
  1283.         if (*position >= 0)
  1284.             *theCount = wordCount;
  1285.         else
  1286.             *theCount = -1;
  1287.         AddAENum(wordCount);
  1288.     } else {
  1289.         /* no words (or letters) in this thing */
  1290.         *theCount = -1;
  1291.     }
  1292.     HSetState(theText, textState);
  1293. }
  1294.  
  1295. void CountShapes(const AEDesc *container, long *theCount, long stopAt, long *position, DescType *whichType)
  1296. {
  1297. #pragma unused (stopAt,position)
  1298.     WindowPtr owner;
  1299.     windowCHandle tempWCH;
  1300.     ShapesHandle  theShapes;
  1301.     mVerboseOutput("\p\nCounting Shapes ")
  1302.     if (container->descriptorType == cWindow || container->descriptorType == cDrawingArea) {
  1303.         owner = *((WindowPtr *)(*(container->dataHandle)));
  1304.         tempWCH = (windowCHandle)GetWRefCon(owner);
  1305.         if(*whichType == cGraphicShape){
  1306.         *theCount = (*tempWCH)->numShapes;
  1307.         } else {
  1308.         /* find specific things */
  1309.         *theCount = 0;
  1310.         theShapes = (*tempWCH)->theShapes;
  1311.         while(theShapes){
  1312.         if((*theShapes)->aeType == *whichType)*theCount +=1;
  1313.         theShapes = (*theShapes)->nextShape;
  1314.         }
  1315.         }
  1316.         
  1317.     } else {
  1318.         /* no luck joe, can't count items from this container */
  1319.         *theCount = -1;
  1320.     }
  1321. }
  1322.  
  1323. /* not yet implemented (and also not installed as a callback) */
  1324. pascal OSErr MyGetMarkTokenProc(const AEDesc *dContainerToken, DescType containerClass, AEDesc *result)
  1325. {
  1326. #pragma unused (dContainerToken,containerClass,result)
  1327.     OSErr myErr = noErr;
  1328.     mVerboseOutput("\p\nGetting a Mark token")
  1329.     return(myErr);
  1330. }
  1331.  
  1332. /* not yet implemented (and also not installed as a callback) */
  1333. pascal OSErr MyMarkProc(const AEDesc *dToken, const AEDesc *markToken, long index)
  1334. {
  1335.     OSErr myErr = noErr;
  1336. #pragma unused (dToken,markToken,index) 
  1337.     mVerboseOutput("\p\nMarking something")
  1338.     return(myErr);
  1339. }
  1340.  
  1341. /* not yet implemented (and also not installed as a callback) */
  1342. pascal OSErr MyAdjustMarksProc(long newStart, long newStop, const AEDesc *markToken)
  1343. {
  1344. #pragma unused (newStart,newStop,markToken)
  1345.     OSErr myErr = noErr;
  1346.     mVerboseOutput("\p\nAdjusting Marks")
  1347.     return(myErr);
  1348. }
  1349.  
  1350. /* This builds our GetData event, based on the things you've set up in the object */
  1351. /* definition dialogs. */
  1352. OSErr BuildGetDataEvent(AppleEvent *thisEvent, short which)
  1353. {
  1354.     OSErr myErr = noErr;
  1355.     AEDesc object;
  1356.     myErr = BuildFullObject(&object, which);
  1357.     myErr = AEPutKeyDesc(thisEvent, keyDirectObject, &object);
  1358.     mAEErrorDisplay("\p putting object", myErr)
  1359.     if (!myErr)
  1360.         AEDisposeDesc(&object);
  1361.     /* one last thing to deal with.  We _can_, if we want, add a keyAERequestedType parameter. */
  1362.     /* this is optional, and specifies the format of the data we'd like to work with. */
  1363.     /* NOTE:  The receiver of this event may ignore this completely.  There may be */
  1364.     /* two reasons for the ignoring.... */
  1365.     /* 1) It's an optional param.  That means the receiver may not even look for it, or */
  1366.     /* know it's there. */
  1367.     /* 2) They may not be able to do it.  They may not be able to coerce their data to the */
  1368.     /* type you'd like to get back.  If so, they'll still send data back, it'll just */
  1369.     /* be in the standard format (as described in the registry) for this event and object. */
  1370.     /*  You will have to do the coercion yourself. */
  1371.     /* By The Way:  If you add system level coercion routines for the types you like to see */
  1372.     /* other apps on the same machine will be able to coerce to your types without having any */
  1373.     /* understanding of them. */
  1374.     /* By The Way II:  If you do not get back the type you want, you should also */
  1375.     /* try and coerce it yourself.  Perhaps you have the correct coercion */
  1376.     /* routine on your machine, one that was not available to the other app. */
  1377.     return(myErr);
  1378. }
  1379.  
  1380. /* This builds our SetData event, based on the things you've set up in the object */
  1381. /* definition dialogs. */
  1382. /* It's a little more complicated, since I have to throw in dialogs to get the  */
  1383. /* new value you want */
  1384.  
  1385. OSErr BuildSetDataEvent(AppleEvent *thisEvent, short which)
  1386. {
  1387.     OSErr myErr = noErr;
  1388.     DialogPtr tdial;
  1389.     short strung;
  1390.     RGBColor oldColor = {0,0,0};
  1391.     RGBColor newColor;
  1392.     Str63 paramString;
  1393.     short hitItem = 0;
  1394.     DescType theDataDesc;
  1395.     short doVis;
  1396.     short *tempShortPtr;
  1397.     Handle theDataHandle = NewHandle(0);
  1398.     AEDesc object;
  1399.     AEDesc dataToSet;
  1400.     /* build the window based on previous things (from dialog) */
  1401.     myErr = BuildFullObject(&object, which);
  1402.     mAEErrorDisplay("\p putting object", myErr)
  1403.     if (!myErr) {
  1404.         /* We've got the object built.  Since this is a SetData event, now we have to add the */
  1405.         /* actual data to set to the thing.  Yes kids, its DialogBox time.... */
  1406.         /* There's nothing interesting here except dialog box handling, if you want to get */
  1407.         /* on to Objects and Æs again, skip ahead to ••here•• */
  1408.         tdial = CommonDStart(kSetDDialog, 0, 0);
  1409.         /* show the correct items */
  1410.         switch (which) {
  1411.             case kWindowBoundsItem:
  1412.                 ShowDItem(tdial, kRectTextItem);
  1413.                 ShowDItem(tdial, kRectEdit1);
  1414.                 ShowDItem(tdial, kRectEdit2);
  1415.                 ShowDItem(tdial, kRectEdit3);
  1416.                 ShowDItem(tdial, kRectEdit4);
  1417.                 break;
  1418.             case kWindowTitleItem:
  1419.                 ShowDItem(tdial, kNewTitleWordsItem);
  1420.                 ShowDItem(tdial, kNewTitleEditLineItem);
  1421.                 break;
  1422.             case kWindowVisiblityItem:
  1423.                 ShowDItem(tdial, kMakeVisItem);
  1424.                 ShowDItem(tdial, kMakeInvisItem);
  1425.                 
  1426.                 break;
  1427.             case kAllText:
  1428.                 ShowDItem(tdial, kNewTextItem);
  1429.                 ShowDItem(tdial, kNewText);
  1430.                 
  1431.                 break;
  1432.             case kWordText:
  1433.                 ShowDItem(tdial, kNewWordItem);
  1434.                 ShowDItem(tdial, kNewText);
  1435.                 
  1436.                 break;
  1437.             case kShapeItem:
  1438.                 ShowDItem(tdial, kNewSBoundsRadio);
  1439.                 ShowDItem(tdial, kNewSColorRadio);
  1440.                 ShowDItem(tdial, kNewSColorButton);
  1441.                 ShowDItem(tdial, kRectEdit1);
  1442.                 ShowDItem(tdial, kRectEdit2);
  1443.                 ShowDItem(tdial, kRectEdit3);
  1444.                 ShowDItem(tdial, kRectEdit4);
  1445.                 HiliteControl(SnatchHandle(tdial, kNewSColorButton),255);
  1446.                 SetCtlValue(SnatchHandle(tdial, kNewSBoundsRadio), true);    
  1447.                 break;
  1448.         }
  1449.         if(which != kShapeItem)
  1450.             strung = which - 1 + kSettingVis;
  1451.         else
  1452.             strung = kSettingShape;
  1453.         GetIndString(paramString, kGeneralStrings, strung);
  1454.         /* I'm going to add a little more text here */
  1455.         if(which == kShapeItem)
  1456.             {DescType shapeTypes[] =  {
  1457.                cGraphicLine, cRectangle, cOval
  1458.             };
  1459.             Str32 theNum;
  1460.             HLock((Handle)gShapeObjSpecHandle);
  1461.             theNum[0] = 6;
  1462.             theNum[1] = ' ';
  1463.             BlockMove((Ptr)&(*gShapeObjSpecHandle)->form,(Ptr)&theNum[2],kFour);
  1464.             theNum[6] = ' ';
  1465.             AppendString(¶mString,&theNum);
  1466.             NumToString((*gShapeObjSpecHandle)->u.index,&theNum);
  1467.             AppendString(¶mString,&theNum);
  1468.             HUnlock((Handle)gShapeObjSpecHandle);
  1469.             
  1470.             }
  1471.         ParamText(paramString, "", "", "");
  1472.         ShowWindow(tdial);
  1473.         DrawDialog(tdial);
  1474.         if (which == kWindowVisiblityItem)
  1475.             SetCtlValue(SnatchHandle(tdial, kMakeVisItem), true);
  1476.         doVis = true;
  1477.         while (hitItem != ok && hitItem != cancel) {
  1478.             ModalDialog((ModalFilterProcPtr)standardDialogFilter, &hitItem);
  1479.             /* the only one that has anything we need to set is the visibility one */
  1480.             if (which == kWindowVisiblityItem) {
  1481.                 if (hitItem == kMakeVisItem || hitItem == kMakeInvisItem) {
  1482.                     SetCtlValue(SnatchHandle(tdial, kMakeVisItem), false);
  1483.                     SetCtlValue(SnatchHandle(tdial, kMakeInvisItem), false);
  1484.                     SetCtlValue(SnatchHandle(tdial, hitItem), true);
  1485.                 }
  1486.             }
  1487.             if (which == kShapeItem) {
  1488.                 switch (hitItem){
  1489.                 Point where;
  1490.                 case kNewSBoundsRadio:
  1491.                 case kNewSColorRadio:
  1492.                     SetCtlValue(SnatchHandle(tdial, kNewSBoundsRadio), false);
  1493.                     SetCtlValue(SnatchHandle(tdial, kNewSColorRadio), false);
  1494.                     SetCtlValue(SnatchHandle(tdial, hitItem), true);
  1495.                 if(GetCtlValue(SnatchHandle(tdial, kNewSColorRadio)))
  1496.                 HiliteControl(SnatchHandle(tdial, kNewSColorButton),0);
  1497.                 else
  1498.                 HiliteControl(SnatchHandle(tdial, kNewSColorButton),255);
  1499.                 break;
  1500.                 case kNewSColorButton:
  1501.             GetIndString(paramString, kGeneralStrings, kNewSCol);
  1502.             where.v = -1;
  1503.             where.h = -1;
  1504.                 GetColor(where,paramString,&oldColor,&newColor);
  1505.                 break;
  1506.                 }
  1507.             }
  1508.         }
  1509.         if (hitItem == 1) {
  1510.             /* and one more switch, pull parameters and create the actual data descriptor that */
  1511.             /* we will pass along in the SetData event. */
  1512.             /* ••here•• */
  1513.             /* pull the various parameters for the data to set, create a Desc to hold them, */
  1514.             /* and add that desc to our event */
  1515.             switch (which) {
  1516.                 Str255 tempString;
  1517.                 long theNum;
  1518.                 register qq;
  1519.                 case kWindowBoundsItem:
  1520.                     theDataDesc = typeQDRectangle;
  1521.                     /* ugg.  Go through all the edit lines and make a rect  */
  1522.                     SetHandleSize(theDataHandle, sizeof(Rect));
  1523.                     tempShortPtr = (short *)*theDataHandle;
  1524.                     HLock(theDataHandle);
  1525.                     for (qq = kRectEdit1; qq < kRectEdit4 + 1; qq++) {
  1526.                         GetIText((Handle)SnatchHandle(tdial, qq), tempString);
  1527.                         StringToNum(tempString, &theNum);
  1528.                         *tempShortPtr = theNum;
  1529.                         tempShortPtr = tempShortPtr + 1;        /* I have had problems with MPW C dealing with a += on a pointer, so I don't do it */
  1530.                     }
  1531.                     break;
  1532.                 case kWindowTitleItem:
  1533.                     theDataDesc = typeChar;
  1534.                     GetIText((Handle)SnatchHandle(tdial, kNewTitleEditLineItem), tempString);
  1535.                     SetHandleSize(theDataHandle, tempString[0]);
  1536.                     /* move the text into the handle */
  1537.                     HLock(theDataHandle);
  1538.                     BlockMove((Ptr)&tempString[1], (Ptr)*theDataHandle, tempString[0]);
  1539.                     /* leave the data handle locked for later */
  1540.                     break;
  1541.                 case kWindowVisiblityItem:
  1542.                     theDataDesc = typeBoolean;
  1543.                     doVis = GetCtlValue(SnatchHandle(tdial, kMakeVisItem));
  1544.                     SetHandleSize(theDataHandle, sizeof(short));        /* a boolean is short length */
  1545.                     HLock(theDataHandle);                   /* for later */
  1546.                     tempShortPtr = (short *)*theDataHandle;
  1547.                     *tempShortPtr = (doVis ? -1 : false);
  1548.                     break;
  1549.                 case kAllText:
  1550.                 case kWordText:
  1551.                     /* these are both the same */
  1552.                     theDataDesc = typeChar;
  1553.                     GetIText((Handle)SnatchHandle(tdial, kNewText), tempString);
  1554.                     SetHandleSize(theDataHandle, tempString[0]);
  1555.                     /* move the text into the handle */
  1556.                     HLock(theDataHandle);
  1557.                     BlockMove((Ptr)&tempString[1], (Ptr)*theDataHandle, tempString[0]);
  1558.                     
  1559.                     break;
  1560.                 case kShapeItem:
  1561.                 /* make the parameters for the shape change */
  1562.                 if(GetCtlValue(SnatchHandle(tdial, kNewSColorRadio))){
  1563.                 /* setting color, the color is in newColor */
  1564.                                     theDataDesc = typeRGBColor;
  1565.                     SetHandleSize(theDataHandle, sizeof(RGBColor));
  1566.                     HLock(theDataHandle);
  1567.                     BlockMove((Ptr)&newColor,*theDataHandle,sizeof(RGBColor));
  1568.  
  1569.                 } else {
  1570.                 /* setting rect, same stuff as window  */
  1571.                                     theDataDesc = typeQDRectangle;
  1572.                     SetHandleSize(theDataHandle, sizeof(Rect));
  1573.                     tempShortPtr = (short *)*theDataHandle;
  1574.                     HLock(theDataHandle);
  1575.                     for (qq = kRectEdit1; qq < kRectEdit4 + 1; qq++) {
  1576.                         GetIText((Handle)SnatchHandle(tdial, qq), tempString);
  1577.                         StringToNum(tempString, &theNum);
  1578.                         *tempShortPtr = theNum;
  1579.                         tempShortPtr = tempShortPtr + 1;        /* I have had problems with MPW C dealing with a += on a pointer, so I don't do it */
  1580.                     }
  1581.  
  1582.  
  1583.  
  1584.                 }
  1585.                 AddShapeProperty(&object,GetCtlValue(SnatchHandle(tdial, kNewSColorRadio)));
  1586.                     break;
  1587.             }
  1588.         } else {
  1589.             myErr = userCanceledErr;
  1590.         }
  1591.         DisposDialog(tdial);
  1592.         /* dialog is gone, the data we want is in theDataHandle, the descriptor type is in theDataDesc, */
  1593.         /* it's time to put the data on the event. */
  1594.         /* create a desc to hold it */
  1595.         if (hitItem == 1) {
  1596.             /* put the object into the event now */
  1597.             myErr = AEPutKeyDesc(thisEvent, keyDirectObject, &object);
  1598.             AEDisposeDesc(&object);
  1599.  
  1600.             
  1601.             
  1602.             myErr = AECreateDesc(theDataDesc, (Ptr)*theDataHandle, GetHandleSize(theDataHandle), &dataToSet);
  1603.             DisposHandle(theDataHandle);                    /* don't need this anymore */
  1604.             if (!myErr) {
  1605.                 /* the desc created fine.  Add this to the event */
  1606.                 myErr = AEPutKeyDesc(thisEvent, keyAEData, &dataToSet);
  1607.                 myErr = AEDisposeDesc(&dataToSet);
  1608.                 /* and the event is all ready to be sent by the caller, so we return */
  1609.             }
  1610.         } else {
  1611.             myErr = userCanceledErr;
  1612.         }
  1613.     }
  1614.     return(myErr);
  1615. }
  1616.  
  1617. /* BuildFullObject builds an object specifier based on the parameters set in */
  1618. /* the object dialogs in this sample. */
  1619. OSErr BuildFullObject(AEDesc *object, short which)
  1620. {
  1621.     OSErr myErr = noErr;
  1622.     AEDesc tempDesc;
  1623.     AEDesc otherTemp;
  1624.     long number1 = 1;
  1625.     DescType Props[3] =  {
  1626.         pBounds, pName, pVisible
  1627.     };
  1628.     
  1629.     BuildWindowObject(object);
  1630.     switch (which) {
  1631.         /* we already have a window object, so for the first three, create a property desc and type */
  1632.         /* since we're only working with the properties of windows */
  1633.         case kWindowBoundsItem:
  1634.         case kWindowTitleItem:
  1635.         case kWindowVisiblityItem:
  1636.             AEDuplicateDesc(object, &otherTemp);
  1637.             myErr = AECreateDesc(typeType, (Ptr)&Props[which - 1], kFour, &tempDesc);
  1638.             myErr = CreateObjSpecifier(typeProperty, &otherTemp, formPropertyID, &tempDesc, true, object);
  1639.             
  1640.             break;
  1641.             
  1642.         case kAllText:
  1643.             /* we're asking for all the text in the window */
  1644.             AEDuplicateDesc(object, &otherTemp);
  1645.             HLock((Handle)gTextObjSpecHandle);
  1646.             if ((*gTextObjSpecHandle)->u.index == 0)
  1647.                 (*gTextObjSpecHandle)->u.index = 1;
  1648.             myErr = AECreateDesc(typeLongInteger, (Ptr)&(*gTextObjSpecHandle)->u.index, sizeof(long), &tempDesc);
  1649.             
  1650.             myErr = CreateObjSpecifier(cText, &otherTemp, formAbsolutePosition, &tempDesc, true, object);
  1651.             
  1652.             HUnlock((Handle)gTextObjSpecHandle);
  1653.             break;
  1654.         case kWordText:
  1655.             /* one word from the window and text object  */
  1656.             AEDuplicateDesc(object, &otherTemp);
  1657.             HLock((Handle)gTextObjSpecHandle);
  1658.             if ((*gTextObjSpecHandle)->u.index == 0)
  1659.                 (*gTextObjSpecHandle)->u.index = 1;
  1660.             myErr = AECreateDesc(typeLongInteger, (Ptr)&(*gTextObjSpecHandle)->u.index, sizeof(long), &tempDesc);
  1661.             
  1662.             myErr = CreateObjSpecifier(cText, &otherTemp, formAbsolutePosition, &tempDesc, true, object);
  1663.             AEDuplicateDesc(object, &otherTemp);
  1664.             if ((*gTextObjSpecHandle)->wordNumber == 0)
  1665.                 (*gTextObjSpecHandle)->wordNumber = 0;
  1666.             myErr = AECreateDesc(typeLongInteger, (Ptr)&(*gTextObjSpecHandle)->wordNumber, sizeof(long), &tempDesc);
  1667.             myErr = CreateObjSpecifier(cWord, &otherTemp, formAbsolutePosition, &tempDesc, true, object);
  1668.             
  1669.             HUnlock((Handle)gTextObjSpecHandle);
  1670.             break;
  1671.             /* for these, we have to add the additional cDrawingArea container */
  1672.         case kShapeItem:
  1673.             AEDuplicateDesc(object, &otherTemp);
  1674.             /* this always defaults to 1, we have only one drawing area per window */
  1675.             AECreateDesc(typeLongInteger, (Ptr)&number1, sizeof(long), &tempDesc);
  1676.             myErr = CreateObjSpecifier(cDrawingArea, &otherTemp, formAbsolutePosition, &tempDesc, true, object);
  1677.             /* now which the thing */
  1678.  
  1679.             myErr = CreateShapeObject(object);
  1680.             break;
  1681.     }
  1682.     mAEErrorDisplay("\p creating object", myErr)
  1683.     
  1684.     return(myErr);
  1685.     
  1686. }
  1687. /* BuildWindowObject builds a window object from null, based on the parameters you have */
  1688. /* specified in the dialog boxes */
  1689. OSErr BuildWindowObject(AEDesc *returnedObject)
  1690. {
  1691.     long theLong = 1;
  1692.     OSErr myErr = noErr;
  1693.     AEDesc containerDesc;
  1694.     AEDesc theIdentifier;
  1695.     /* establish some defaults so the object will be right even if the Window Object dialog hasn't been used */
  1696.     DescType theForm = formAbsolutePosition;
  1697.     long number1 = 1;
  1698.     HLock((Handle)gWindObjSpecHandle);
  1699.     switch ((*gWindObjSpecHandle)->form) {
  1700.         case formAbsolutePosition:
  1701.             theForm = formAbsolutePosition;
  1702.             /* make an abs for spec */
  1703.             /* What??  What that comment really meant, was create an object specifier for a window */
  1704.             /* based on the data we have stored in our window descriptor object, that's */
  1705.             /* set with the dialog, based on absolute position or name */
  1706.             if ((*gWindObjSpecHandle)->u.index == 0)
  1707.                 (*gWindObjSpecHandle)->u.index = 1;
  1708.             myErr = AECreateDesc(typeLongInteger, (Ptr)&(*gWindObjSpecHandle)->u.index, sizeof(long), &theIdentifier);
  1709.             
  1710.             break;
  1711.         case formName:
  1712.             /* make an object specifier based on window name */
  1713.             
  1714.             theForm = formName;
  1715.             myErr = AECreateDesc(typeChar, (Ptr)&(*gWindObjSpecHandle)->u.name[1], (*gWindObjSpecHandle)->u.name[0],
  1716.                                  &theIdentifier);
  1717.             break;
  1718.         default:
  1719.             /* defaults to a byindex, window one spec if nothing else has been set up */
  1720.             myErr = AECreateDesc(typeLongInteger, (Ptr)&number1, sizeof(long), &theIdentifier);
  1721.             break;
  1722.     }
  1723.     mAEErrorDisplay("\p creating formProperty for this thing", myErr)
  1724.     /* Every event will act on a window */
  1725.     
  1726.     /* we're always adding windows from nul, so duplicate nul */
  1727.     AEDuplicateDesc(&gNullDesc, &containerDesc);
  1728.     
  1729.     /* build a window object contained in nul */
  1730.     myErr = CreateObjSpecifier(cWindow, &containerDesc, theForm, &theIdentifier, true, returnedObject);
  1731.     mAEErrorDisplay("\p creating window object", myErr)
  1732.     HUnlock((Handle)gWindObjSpecHandle);
  1733.     return(myErr);
  1734. }
  1735.  
  1736. /* CreateShapeObject attached the shape object to the basic specifier */
  1737. /* already created (nul>cWindow>cDrawingArea) */
  1738. /* Since all my shapes are basically the same, we have one routine to do this for us */
  1739.  
  1740. OSErr CreateShapeObject(AEDesc *theContainer)
  1741. {
  1742.     AEDesc tempDesc;
  1743.     AEDesc indexDesc;
  1744.     DescType theType;
  1745.     OSErr myErr = noErr;
  1746.     HLock((Handle)gShapeObjSpecHandle);
  1747.     theType = (*gShapeObjSpecHandle)->form;
  1748.     /* duplicate what we have so far */
  1749.     AEDuplicateDesc(theContainer, &tempDesc);
  1750.     /* In this example, we're only implementing a ByIndex form for graphics, so... */
  1751.     if ((*gShapeObjSpecHandle)->u.index == 0)
  1752.         (*gShapeObjSpecHandle)->u.index = 1;
  1753.     myErr = AECreateDesc(typeLongInteger, (Ptr)&(*gShapeObjSpecHandle)->u.index, sizeof(long), &indexDesc);
  1754.     myErr = CreateObjSpecifier(theType, &tempDesc, formAbsolutePosition, &indexDesc, true, theContainer);
  1755.     HUnlock((Handle)gShapeObjSpecHandle);
  1756.     return(myErr);
  1757. }
  1758.  
  1759. /* AddShapeProperty tacks a property specifier onto an already created shape specifier */
  1760. OSErr AddShapeProperty(AEDesc *theObject,Boolean which)
  1761. {
  1762. AEDesc tempDesc,otherTemp;
  1763. DescType theProp;
  1764. OSErr myErr = noErr;
  1765. if(which)
  1766.     theProp = pPenColor;
  1767. else
  1768.     theProp = pBounds;
  1769. myErr = AEDuplicateDesc(theObject, &otherTemp);
  1770. myErr |= AECreateDesc(typeType,(Ptr) &theProp, kFour, &tempDesc);
  1771. myErr |= CreateObjSpecifier(typeProperty, &otherTemp, formPropertyID, &tempDesc, true, theObject);
  1772.  
  1773.  
  1774.  
  1775. return(myErr);
  1776. }
  1777.  
  1778.  
  1779.  
  1780. #undef __AEOBJ__
  1781.